﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;

namespace ML.Utils
{
    /// <summary>
    ///  枚举辅助类
    /// </summary>
    public static class PublicEnumHelper
    {
        /// <summary> 
        /// 获得枚举类型数据项（不包括空项）
        /// </summary> 
        /// <param name="enumType">枚举类型</param> 
        /// <returns></returns> 
        public static IList<BaseEnum> GetEnumList(this Type enumType)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException();

            IList<BaseEnum> list = new List<BaseEnum>();

            // 获取Description特性 
            Type typeDescription = typeof(DescriptionAttribute);
            // 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                BaseEnum mb = new BaseEnum();
                if (!field.FieldType.IsEnum)
                    continue;

                // 获取枚举值
                mb.Id = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);

                // 不包括空项
                if (mb.Id >= 0)
                {
                    object[] array = field.GetCustomAttributes(typeDescription, false);

                    if (array.Length > 0) mb.Name = ((DescriptionAttribute)array[0]).Description;
                    else mb.Name = field.Name; //没有描述，直接取值
                    //添加到列表
                    list.Add(mb);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据枚举值获取对应Description 属性
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetEnumDesc(Type enumType, int? id)
        {
            Type typeDescription = typeof(DescriptionAttribute);
            FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    if (Convert.ToInt16(enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)) ==
                        id)
                    {
                        object[] arr = field.GetCustomAttributes(typeDescription, true);
                        if (arr.Length > 0)
                        {
                            var descriptionAttribute = (DescriptionAttribute)arr[0];
                            return descriptionAttribute.Description;
                        }
                    }
                }
            }
            return strText;
        }

        /// <summary>  
        /// 获取枚举变量值的 Description 属性  
        /// </summary>  
        /// <param name="obj">枚举变量</param>  
        /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性，而不是当前的属性或枚举变量值的 Description 属性</param>  
        /// <returns>如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称</returns>  
        public static string GetDescription(this object obj, bool isTop)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                Type enumType = obj.GetType();
                DescriptionAttribute dna = null;
                if (isTop)
                {
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    FieldInfo fi = enumType.GetField(Enum.GetName(enumType, obj));
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
                        fi, typeof(DescriptionAttribute));
                }
                if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
                    return dna.Description;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return obj.ToString();
        }
    }

    public class BaseEnum
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

}